<?php

class FileLogger {
    private $filePointer;
    private $logFile;
    private $message = '';
    private $type = '???';
   

    public function __construct() {
        $this->logFile = Config::Get('PATH_LOG_FILE') . date("Ymd") . '.txt';
    }

    /**
     * Zapis wiadomosci $message typu $this->type (DEBUG, WARN, INFO, ERROR, FATAL) do pliku 
     * @param <string> $message 
     */
    public function Write($message, $type='DEBUG') {
        if (!$this->filePointer)
            $this->Open();

        $this->SetMessage($message, $type);
        fwrite($this->filePointer, "$this->message\n");
    }

    // open log file  
    private function Open() {
        $this->filePointer = fopen($this->logFile, 'a') or exit("Nie można otworzyć pliku: $this->logFile!");
    }

    /**
     * @param <string> $message 
     */
    private function SetMessage($message) {
        $this->message = '[' . date('Y-m-d H:i:s') . '] ' . '['.$this->type.'] ' . $message;
    }
    
    public function SetType($type) {
        $this->type = $type;
    }
    /**
     * @param <string> $file 
     */
    public function SetLogFile($file) {
        $this->logFile = $file;
    }

    /**
     * Zamykam wczesniej otwarty plik
     */
    public function __destruct() {
        if (isset($this->filePointer))
            fclose($this->filePointer);
    }

}

?>
